$(function () {
    // 获取cookie
    function getCookie(cookie_name){
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(cookie_name);
        if (cookie_pos != -1){
            cookie_pos += cookie_name.length + 1;
            var cookie_end = allcookies.indexOf(";", cookie_pos);
            if (cookie_end == -1){cookie_end = allcookies.length;}
            var value = unescape(allcookies.substring(cookie_pos, cookie_end));}
        return value;
    }
    // 校区初始化
    var city_id=$("#campus_text").find("option:selected").val().split("_")[0];
    $.ajax({
        type: "post",
        url: "/api/school/list",
        data: {cityId:city_id},
        dataType: "json",
        success:function (response) {
            for (var i = 0; i < response.length; i++) {
                $("#school_text").append("<option value="+response[i].id+"_"+response[i].name+">"+response[i].name+"</option>")
            };
        }
    });
    $("#campus_text").change(function () {
        var  cityId=$(this).val();
        $.ajax({
            type: "post",
            url: "/api/school/list",
            data: {cityId:cityId},
            dataType: "json",
            success:function (response) {
               $("#school_text").html("");
                for (var i = 0; i < response.length; i++) {
                    $("#school_text").append("<option value="+response[i].id+"_"+response[i].name+">"+response[i].name+"</option>")
                };
            }
        });
    })
    function isPositiveInteger(s){//是否为正整数
        var re = /^[0-9]+$/ ;
        return re.test(s)
    }
    //提交信息
    var  score_file=Number($("#score_file").val());
    var  chinese=Number($("#chinese").val());
    var  mathematics=Number($("#mathematics").val());
    var  english=Number($("#english").val());
    var  physics=Number($("#physics").val());
    var  chemical=Number($("#chemical").val());
    var  creature=Number($("#creature").val());
    var  history=Number($("#history").val());
    var  geography=Number($("#geography").val());
    var  politik=Number($("#politik").val());
    var  total=Number($("#total").val());
    $('#subBtn').click(function(){
        //TODO 输入的中考成绩不能低于430 不能高于570
        if(score_file<430 &&score_file>570){
            alert("中考成绩的填写范围430到570！")
        }
        if(isPositiveInteger(chinese)&& isPositiveInteger( mathematics)&& isPositiveInteger(english)&& isPositiveInteger( physics)&& isPositiveInteger(chemical)&& isPositiveInteger(creature)&& isPositiveInteger(history)&& isPositiveInteger(geography)&& isPositiveInteger(politik) && isPositiveInteger(total)){
            var params=$('#form').serialize();
            console.log(params)
            $.ajax({
                type: "post",
                url: "/api/create",
                data: params,
                dataType: "json",
                success:function (response){
                    document.cookie = 'result='+response.result;// 保存一个值
                    $("#zhezhao_box").css({display:"block"});
                }
            });
        }else {
            alert("请填写大于等于0的数字！")
        }
    });
  $("#total").blur(function () {
      var  chinese=Number($("#chinese").val());
      var  mathematics=Number($("#mathematics").val());
      var  english=Number($("#english").val());
      var  physics=Number($("#physics").val());
      var  chemical=Number($("#chemical").val());
      var  creature=Number($("#creature").val());
      var  history=Number($("#history").val());
      var  geography=Number($("#geography").val());
      var  politik=Number($("#politik").val());
      $("#total").val(chinese+mathematics+english+physics+chemical+creature+history+geography+politik)
      return
  });
})
// 朋友圈分享
// 导入的数据

        var timestamp=Date.parse(new Date());
        var nonceStr=getComputedStyle(6);
        var url="";
        var signature="";
        var jschars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        function generateMixed(n) {
        var res = "";
        for(var i = 0; i < n ; i ++) {
            var id = Math.ceil(Math.random()*35);
            res += jschars[id];
        }
            return res;
        }

        wx.config({
            debug: true,
            appId: 'wx0af6bb209cdb85e2',
            timestamp: timestamp,
            nonceStr: nonceStr,
            signature: signature,
            jsApiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'onMenuShareQZone',
                'hideMenuItems',
                'showMenuItems',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'translateVoice',
                'startRecord',
                'stopRecord',
                'onVoiceRecordEnd',
                'playVoice',
                'onVoicePlayEnd',
                'pauseVoice',
                'stopVoice',
                'uploadVoice',
                'downloadVoice',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'getNetworkType',
                'openLocation',
                'getLocation',
                'hideOptionMenu',
                'showOptionMenu',
                'closeWindow',
                'scanQRCode',
                'chooseWXPay',
                'openProductSpecificView',
                'addCard',
                'chooseCard',
                'openCard'
            ]
        });

        var imgUrl ="http://oa.zhizhuoedu.cn/image/logo.png";
        var lineLink = 'http://asp.koudaimaster.net/';
        var descContent = "面对6选3，排名更重要,期中考试结束，无法获知孩子的成绩排名？瑶瑶老师给你支招！只需要输入孩子期中考试成绩，即可科学预测孩子在全市的排名定位";
        var shareTitle = '致高一家长，点击查看孩子期中成绩在全市的排名定位！'
        var appid = 'wx0af6bb209cdb85e2';

        wx.ready(function(){
            // 2. 分享接口
            // 2.1 监听“分享给朋友”，按钮点击、自定义分享内容及分享结果接口

            wx.onMenuShareAppMessage({
                title: shareTitle,
                desc: descContent,
                link: lineLink,
                imgUrl: imgUrl,
                dataUrl:"",
                trigger: function (res) {
                    // 不要尝试在trigger中使用ajax异步请求修改本次分享的内容，因为客户端分享操作是一个同步操作，这时候使用ajax的回包会还没有返回
                    alert('用户点击发送给朋友');
                },
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });

            // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
            wx.onMenuShareTimeline({
                title: shareTitle,
                link: lineLink,
                imgUrl: imgUrl,
                trigger: function (res) {
                    // 不要尝试在trigger中使用ajax异步请求修改本次分享的内容，因为客户端分享操作是一个同步操作，这时候使用ajax的回包会还没有返回
                    alert('用户点击分享到朋友圈');
                },
                success: function (res) {
                    alert('已分享');
                    var result=getCookie('result');
                    if(result>1){
                        window.location.href="http://oa.zhizhuoedu.cn/result_2"
                    }else if(-10<result &&result<1){
                        window.location.href="http://oa.zhizhuoedu.cn/result_1"
                    }else if(result<-10){
                        window.location.href="http://oa.zhizhuoedu.cn/result_3"
                    }
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });

            // 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口
            wx.onMenuShareQQ({
                title: shareTitle,
                desc: descContent,
                link: lineLink,
                imgUrl: imgUrl,
                trigger: function (res) {
                    alert('用户点击分享到QQ');
                },
                complete: function (res) {
                    alert(JSON.stringify(res));
                },
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });

            // 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口
            wx.onMenuShareWeibo({
                title: shareTitle,
                desc:descContent,
                link: lineLink,
                imgUrl: imgUrl,
                trigger: function (res) {
                    alert('用户点击分享到微博');
                },
                complete: function (res) {
                    alert(JSON.stringify(res));
                },
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });

            // 2.5 监听“分享到QZone”按钮点击、自定义分享内容及分享接口
            wx.onMenuShareQZone({
                title: shareTitle,
                desc: descContent,
                link: lineLink,
                imgUrl: imgUrl,
                trigger: function (res) {
                    alert('用户点击分享到QZone');
                },
                complete: function (res) {
                    alert(JSON.stringify(res));
                },
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });
        });

        wx.error(function(res){
            alert(res);
            // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
        });



